/*
 PureMVC AS3 Demo - Flex CafeTownsend
 Copyright (c) 2007-08 Michael Ramirez <michael.ramirez@puremvc.org>
 Parts Copyright (c) 2005-07 Adobe Systems, Inc. 
 Your reuse is governed by the Creative Commons Attribution 3.0 License
 */
/*
Copyright 2011 The Apache Software Foundation

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package idv.takeshi.view
{
	import flash.events.Event;
    import org.puremvc.as3.interfaces.*;
	import org.puremvc.as3.patterns.mediator.Mediator;

    import idv.takeshi.ApplicationFacade;
	import idv.takeshi.view.components.*;
	import idv.takeshi.model.PersonProxy;
    
    /**
     * A Mediator for interacting with the PersonList component
     */
    public class PersonListMediator extends Mediator implements IMediator
    {
        // Cannonical name of the Mediator
        public static const NAME:String = "PersonListMediator";
        
        /**
         * Constructor. 
         */
        public function PersonListMediator( viewComponent:Object ) 
        {
            // pass the viewComponent to the superclass where 
            // it will be stored in the inherited viewComponent property
            super( NAME, viewComponent );
			
			personProxy = PersonProxy( facade.retrieveProxy( PersonProxy.NAME ) );
			
			personList.addEventListener( PersonList.APP_LOGOUT, logout );
			personList.addEventListener( PersonList.ADD_PERSON, addEmployee );
			personList.addEventListener( PersonList.UPDATE_PERSON, updatePerson );
        }

        /**
         * List all notifications this Mediator is interested in.
         * <P>
         * Automatically called by the framework when the mediator
         * is registered with the view.</P>
         * 
         * @return Array the list of Nofitication names
         */
        override public function listNotificationInterests():Array 
        {
            return [ApplicationFacade.LOAD_PERSONS, 
				ApplicationFacade.LOAD_PERSONS_SUCCESS,
				ApplicationFacade.LOAD_PERSONS_FAILED];
        }

        /**
         * Handle all notifications this Mediator is interested in.
         * <P>
         * Called by the framework when a notification is sent that
         * this mediator expressed an interest in when registered
         * (see <code>listNotificationInterests</code>.</P>
         * 
         * @param INotification a notification 
         */
        override public function handleNotification( note:INotification ):void 
        {
            switch ( note.getName() ) 
			{
				
				case ApplicationFacade.LOAD_PERSONS:
					personProxy.loadPersons();
					sendNotification(ApplicationFacade.LOAD_ROLES);
					break;
                case ApplicationFacade.LOAD_PERSONS_SUCCESS:
                    personList.gridPersons.dataProvider = personProxy.getData();
                    break;
				case ApplicationFacade.LOAD_PERSONS_FAILED:
					personList.error.text = String(personProxy.getData());
					break;
            }
        }
                    
        /**
         * Cast the viewComponent to its actual type.
         * 
         * <P>
         * This is a useful idiom for mediators. The
         * PureMVC Mediator class defines a viewComponent
         * property of type Object. </P>
         * 
         * <P>
         * Here, we cast the generic viewComponent to 
         * its actual type in a protected mode. This 
         * retains encapsulation, while allowing the instance
         * (and subclassed instance) access to a 
         * strongly typed reference with a meaningful
         * name.</P>
         * 
         * @return PersonList the viewComponent cast to PersonList
         */
        protected function get personList():PersonList
		{
            return viewComponent as PersonList;
        }
		
		private function logout( event:Event = null ):void
		{
			sendNotification( ApplicationFacade.APP_LOGOUT );
		}
		
		private function addEmployee( event:Event = null ):void
		{
			sendNotification( ApplicationFacade.ADD_PERSON );
		}
		
		private function updatePerson( event:Event = null ):void
		{
			sendNotification( ApplicationFacade.UPDATE_PERSON, personList.gridPersons.selectedItem);
		}
		
		private var personProxy:PersonProxy;
    }
}
